package com.smfx.yxt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AdminUser;
import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.Constants;
import com.smfx.yxt.model.ClassTeachers;
import com.smfx.yxt.model.ComboBox;
import com.smfx.yxt.model.OrganizeGroup;
import com.smfx.yxt.model.StudentOrganize;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.model.TeacherOrganizeGroup;
import com.smfx.yxt.reqVo.BaseQueryVO;
import com.smfx.yxt.reqVo.StudentReqVO;
import com.smfx.yxt.reqVo.TeacherReqVO;
import com.smfx.yxt.service.IClassTeachersService;
import com.smfx.yxt.service.IKccsCourseInfoService;
import com.smfx.yxt.service.IOrganizeDepartService;
import com.smfx.yxt.service.IOrganizeGroupService;
import com.smfx.yxt.service.IStudentService;
import com.smfx.yxt.service.ISubjectInfoService;
import com.smfx.yxt.service.ISysDictDataService;
import com.smfx.yxt.service.ITeacherService;
import com.smfx.yxt.service.ITermInfoService;
import com.smfx.yxt.utils.StringUtil;
import com.smfx.yxt.view.GradeTeacherView;
import com.smfx.yxt.view.TeacherInfoView;
import com.smfx.yxt.wechat.utils.WechatGlobalMap;

@Controller
public class CompentController extends BaseController{
	
	@Autowired
	private IOrganizeDepartService organizeDepartService;
	@Autowired
	private IOrganizeGroupService organizeGroupService;
	@Autowired
	private IStudentService studentService;
	@Autowired
	private ITeacherService teacherService;
	@Autowired
	private IClassTeachersService classTeachersService;
	@Autowired
	private ITermInfoService termInfoService;
	@Autowired
	private ISysDictDataService sysDictDataService;
	@Autowired
	private IKccsCourseInfoService kccsCourseInfoService;
	@Autowired
	private ISubjectInfoService subjectInfoService;
	
	@RequestMapping("organizeDepart/comboBox")
	@ResponseBody Object organizeDepartComboBox() {
		return organizeDepartService.selectComboBox();
	}
	
	@RequestMapping("organizeGroup/comboBox")
	@ResponseBody Object organizeGroupComboBox() {
		String groupCode = StringUtil.emptyToNull(request.getParameter("groupCode"));
		OrganizeGroup organizeGroup = new OrganizeGroup();
		organizeGroup.setGroupCode(groupCode);
		return organizeDepartService.selectGroupComboBox(organizeGroup);
	}
	
	/**
	 * 学校分组架构
	 * @return
	 */
	@RequestMapping("teacherOrganizeGroup")
	@ResponseBody Object teacherOrganizeGroup() {
		AppUser appUser = getAppUser();
		List<TeacherOrganizeGroup> list = organizeGroupService.fillTeacherOrganizeGroup(appUser.getTeacher().getSchoolId());
		return list;
	}
	
	@RequestMapping("teacherOrganizeGroup/{groupId}")
	@ResponseBody Object teacherOrganizeGroupId(@PathVariable Integer groupId) {
		return teacherService.selectListForOrganizeGroup(groupId);
	}
	
	/**
	 * 学校部门架构
	 * @return
	 */
	@RequestMapping("teacherOrganizeDepart")
	@ResponseBody Object teacherOrganizeDepart() {
		AppUser appUser = getAppUser();
		List<TeacherOrganizeGroup> list = organizeDepartService.fillTeacherOrganize(appUser.getTeacher().getSchoolId());
		return list;
	}
	
	@RequestMapping("teacherOrganizeDepart/{positionIds}")
	@ResponseBody Object teacherOrganizePositionId(@PathVariable String positionIds) {
		AppUser appUser = getAppUser();
		List<TeacherOrganizeGroup> list = organizeDepartService.
				fillTeacherOrganizeByPositionIds(positionIds, 
						appUser.getTeacher().getSchoolId());
		return list;
	}
	
	/**
	 * 学生组织架构
	 * @return
	 */
	@RequestMapping("studentOrganize")
	@ResponseBody Object studentOrganize() {
		List<StudentOrganize> list = studentService.fillStudentOrganize(
				getCurrentTerm().getFinishYear(),getAppUser().getStudyType());
		return list;
	}
	
	/**
	 * 班级架构
	 * @return
	 */
	@RequestMapping("classOrganize")
	@ResponseBody Object classOrganize() {
		AppUser appUser = getAppUser();
		StudentReqVO studentInfo = new StudentReqVO();
		studentInfo.setSchoolId(appUser.getTeacher().getSchoolId());
		studentInfo.setStudyType(appUser.getStudyType());
		studentInfo.setGrade(appUser.getTeacher().getGrade());
		studentInfo.setClazz(appUser.getTeacher().getClazz());
		return studentService.selectListByParam(studentInfo);
	}
	
	@RequestMapping("classList")
	@ResponseBody Object classList() {
		AppUser appUser = getAppUser();
		BaseQueryVO vo = new BaseQueryVO();
		vo.setFinishYear(getCurrentTerm().getFinishYear());
		vo.setSchoolId(appUser.getTeacher().getSchoolId());
		List<ClassTeachers> list = classTeachersService.selectClassList(vo);
		return getGradeClassList(list);
	}
	
	/**
	 * 老师列表
	 */
	@RequestMapping("teacherList")
	@ResponseBody Object teacherList() {
		AppUser appUser = getAppUser();
		TeacherReqVO vo = new TeacherReqVO();
		vo.setFinishYear(getCurrentTerm().getFinishYear());
		vo.setSchoolId(appUser.getTeacher().getSchoolId());
		List<TeacherInfo> list = teacherService.selectListForAll(vo);
		List<GradeTeacherView> viewList = new ArrayList<>();
		Map<String, GradeTeacherView> map = new HashMap<>();
		for(TeacherInfo teacher : list) {
			String gradeKey = String.valueOf(teacher.getGrade()) + teacher.getStudyType();
			if(!map.containsKey(teacher.getGrade())) {
				GradeTeacherView view = new GradeTeacherView();
				List<TeacherInfo> list2 = new ArrayList<>();
				list2.add(teacher);
				view.setStudyType(teacher.getStudyType());
				view.setGrade(teacher.getGrade());
				view.setTeacherList(list2);
				map.put(gradeKey, view);
				viewList.add(map.get(gradeKey));
				continue;
			}
			((GradeTeacherView)map.get(gradeKey)).getTeacherList().add(teacher);
		}
		map = null;
		return viewList;
	}
	
	/**
	 * 老师带班列表
	 * @return
	 */
	@RequestMapping("teacherClass")
	@ResponseBody Object teacherClass() {
		AppUser appUser = getAppUser();
		List<TeacherInfoView> list = teacherService.selectClazzListByOpenid(appUser.getOpenid());
		if(list != null && list.size() > 0) {
			return list;
		}
		return studentService.fillStudentOrganize(getCurrentTerm().getFinishYear(), getAppUser().getStudyType());
	}
	
	/**
	 * 我的班级
	 * @return
	 */
	@RequestMapping("myClass")
	@ResponseBody Object myClass() {
		AppUser appUser = getAppUser();
		List<TeacherInfoView> list = teacherService.selectClazzListByOpenid(appUser.getOpenid());
		return list;
	}
	
	@RequestMapping("gradeList")
	@ResponseBody Object gradeList() {
		Integer studyType = getAppUser().getStudyType();
		if(studyType == 1) return getCurrentTerm().getMiddleGradeMap();
		return getCurrentTerm().getHighGradeMap();
	}
	
	@RequestMapping("studnetGradeByTermYear")
	@ResponseBody Object studnetGradeByTermYear() {
		String studyType = request.getParameter("studyType");
		String termYear = request.getParameter("termYear");
		List<ComboBox> list = new ArrayList<>();
		if("1".equals(studyType)) {
			list.add(new ComboBox(termYear + "," + 1, "初一"));
			list.add(new ComboBox((Integer.valueOf(termYear).intValue() - 1) + "," + 1, "初二"));
			list.add(new ComboBox((Integer.valueOf(termYear).intValue() - 2) + "," + 1, "初三"));
		}else {
			list.add(new ComboBox(termYear + "," + 2, "高一"));
			list.add(new ComboBox((Integer.valueOf(termYear).intValue() - 1) + "," + 2, "高二"));
			list.add(new ComboBox((Integer.valueOf(termYear).intValue() - 2) + "," + 2, "高三"));
		}
		return list;
	}
	
	@RequestMapping("gradeByTermYear")
	@ResponseBody Object gradeByTermYear(Integer termYear) {
		if(termYear == null){
			termYear = getCurrentTerm().getTermKey();
		}
		List<ComboBox> list = new ArrayList<>();
		list.add(new ComboBox(termYear + "," + 1, "初一"));
		list.add(new ComboBox(String.valueOf(termYear.intValue() - 1) + "," + 1, "初二"));
		list.add(new ComboBox(String.valueOf(termYear.intValue() - 2) + "," + 1, "初三"));
		list.add(new ComboBox(String.valueOf(termYear) + "," + 2, "高一"));
		list.add(new ComboBox(String.valueOf(termYear.intValue() - 1) + "," + 2, "高二"));
		list.add(new ComboBox(String.valueOf(termYear.intValue() - 2) + "," + 2, "高三"));
		return list;
	}

	@RequestMapping("getFullClazz")
	@ResponseBody Object getFullClazz() {
		String grade = request.getParameter("grade");
		String[] array = grade.split(",");
		return studentService.getFullClazz(Integer.valueOf(array[0]), Integer.valueOf(array[1]));
	}
	
	@RequestMapping("subjectList")
	@ResponseBody Object subjectList() {
		return WechatGlobalMap.get(Const.CACHE_SUBJECT).getValue();
	}
	
	@RequestMapping("subjectList1")
	@ResponseBody Object subjectList1() {
		return subjectInfoService.selectList();
	}
	
	@RequestMapping("valid")
	@ResponseBody Object valid() {
		List<ComboBox> list = new ArrayList<>();
		list.add(new ComboBox("N", "有效"));
		list.add(new ComboBox("Y", "失效"));
		return list;
	}
	
	@RequestMapping("termList")
	@ResponseBody Object selectList() {
		return termInfoService.selectList();
	}
	
	@RequestMapping("studentTermList")
	@ResponseBody Object studentTermList(Integer grade) {
		return termInfoService.selectStudentTermList(grade);
	}
	
	@RequestMapping("dictData/dictCode")
	@ResponseBody Object dictDataDictCode(String dictCode) {
		return sysDictDataService.selectListByDictCode(dictCode);
	}
	
	@RequestMapping("studyCourse")
	@ResponseBody Object studyCourse(BaseQueryVO vo){
		return kccsCourseInfoService.selectSignCourseList(vo);
	}
	
	@RequestMapping("teacherAll")
	@ResponseBody Object teacherList(HttpServletRequest request, TeacherReqVO vo) {
		AdminUser user = (AdminUser) request.getSession().getAttribute(Constants.ADMIN_USER);
		vo.setSchoolId(user.getTeacherInfo().getSchoolId());
		vo.setTeacherName(vo.getQ());
		List<TeacherInfo> list = new ArrayList<>();
		TeacherInfo teacher = new TeacherInfo();
		teacher.setId(0);
		teacher.setName("请选择");
		list.add(teacher);
		List<TeacherInfo> resultList = teacherService.selectByParam(vo);
		if(resultList != null && resultList.size() > 0) {
			list.addAll(resultList);
		}
		return list;
	}
}
